﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ARCam3D.Imaging
{
    unsafe public class GrayscaleImage : AbstractImage
    {

        private IntPtr dataPtr;

        public GrayscaleImage(int width, int height)
            : base(width, height)
        {
            this.dataPtr = Marshal.AllocHGlobal(width * height * sizeof(byte));
        }

        ~GrayscaleImage()
        {
            if (dataPtr != null && dataPtr != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(dataPtr);
            }
        }

        public IntPtr DataPtr
        {
            get { return dataPtr; }
        }

        public byte GetValue(int x, int y)
        {
            return ((byte*)dataPtr.ToPointer())[y * width + x];
        }

        public void SetValue(int x, int y, byte value)
        {
            ((byte*)dataPtr.ToPointer())[y * width + x] = value;
        }
    }
}
